package com.yzc.interceptor;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
@RequiredArgsConstructor
public class TokenService {

    private final RedisTemplate<String, String> redisTemplate;

    public String getLatestTokenByUserId(Long userId) {
        String userTokenKey = "user_tokens:" + userId;
        // 获取最新的 Token（按时间戳降序排序）
        Set<String> tokens = redisTemplate.opsForZSet().reverseRange(userTokenKey, 0, 0);
        if (tokens == null || tokens.isEmpty()) {
            return null;
        }
        return tokens.iterator().next();
    }
}